<template>
  <div class="login-page">
    <el-card class="el-card">
      <!-- header插槽 -->
      <template #header>
        后台管理系统
        <router-link class="link" to="/login">去登录</router-link>
        </template>
      <!-- 表单 -->
      <el-form autocomplete="off" :model="form" :rules="rules" ref="myForm">
        <el-form-item label="用户名：" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码：" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
        </el-form-item>

         <el-form-item label="密码：" prop="repassword">
          <el-input type="password" v-model="form.repassword" placeholder="请再次输入密码"></el-input>
        </el-form-item>

        <el-form-item class="tc">
          <el-button type="primary" @click="register" class="login">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="logo"></div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  name: "LoginIndex",
  data () {
    return {
        form: {
            username: '',
            password: '',
            repassword: ''
        },
        rules: {
            // 设置对应字段的校验规则
            username: [
                { required: true, message: '请输入用户名', trigger: ['blur','change'] },
                { min: 5, max: 11, message: '长度 5 到 11 个字符', trigger: ['blur'] }
            ],
            password: [
                { required: true, message: '请输入密码', trigger: ['blur','change'] },
                { pattern: /^\w{5,11}$/, message: '长度 5 到 10 位的密码', trigger: ['blur'] }
            ],
            repassword: [
               { required: true, message: '请输入密码', trigger: ['blur','change'] },
               { pattern: /^\w{5,11}$/, message: '长度 5 到 10 位的密码', trigger: ['blur'] }
            ]
        }
    }
  },
  methods: {
   async register () {
       try {
          const valid = await this.$refs.myForm.validate()
          if (valid) {
            if (this.form.password !== this.form.repassword) {
            return  this.$message.warning('两次密码不相同,请检查!')
            }
            const res = await register(this.form)
            console.log(res)
           if (res.code === 0) {
            this.$message.success('注册成功')
            this.$refs.myForm.resetFields()
            this.$router.push('/login')
           }
            this.$message.error(res.message)
          }
       } catch (e) {
          console.log(e)
       }
   },
  }
};
</script>

<style lang="less" scoped>
.login-page {
  position: relative;
  min-height: 100vh;
  background: url(https://lanhu.oss-cn-beijing.aliyuncs.com/SketchPnge466a35a7e55683e4f024770ac7a3c8f6e08659a88a7b402d5c5b874853aa1c2) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    margin: 0 auto;

    // 深度作用选择器 scss  ::v-deep   less /deep/
    ::v-deep .el-card__header {
      height: 80px;
      text-align: center;
      font-size: 25px;
    }
  }
   .el-form {
    padding: 0 20px;
  }
  
  .tc {
    text-align: center;
  }

  .login {
    width: 100%;
    background-color: #4F81FF;
  }

  .link {
    display: block;
    margin-top: 5px;
    color: #4F81FF;
    font-size: 12px;
    text-decoration: none;
  }

  .logo {
    position: absolute;
    top: 30px;
    left: 50px;
    width: 100px;
    height: 40px;
    background: url(https://lanhu.oss-cn-beijing.aliyuncs.com/SketchPngd49eceef977cbc6df84eac9150a770d8e5f2326e08feb8d1bd5055a9fa38dc02) no-repeat center / cover;
  }
}
</style>